

from analyse.kline_handler.enum import ShapeType, YinYangType
from analyse.kline_handler.handler import KlineHandler


class KlineComposition(KlineHandler):
     def handle(self, kline):
        high=kline["high"]
        low=kline["low"]
        open=kline["open"]
        close=kline["close"]
        # 计算上影线
        upperShadow=high-max(open,close)
        # 计算下影线
        lowerShadow=min(open,close)-low
        # 计算实体长度
        body=abs(open-close)
        bodyPercent=body/kline["preClose"]*100

        kline["body"]=body
        kline["upperShadow"]=upperShadow
        kline["lowerShadow"]=lowerShadow
        kline["bodyPercent"]=bodyPercent

        

        if open>close:
            self.compound_kline_type(kline,YinYangType["阴"].value*100000) #阴线
        else:
            self.compound_kline_type(kline,YinYangType["阳"].value*100000) #阳线

        if bodyPercent==0:
            self.compound_kline_type(kline,ShapeType["一字"].value*10000) #一字
        elif bodyPercent<0.6:
            self.compound_kline_type(kline,ShapeType["星线"].value*10000) #星线
        elif bodyPercent>=0.6 and bodyPercent<=3:
            self.compound_kline_type(kline,ShapeType["小"].value*10000) #小阴、小阳
        elif bodyPercent>3 and bodyPercent<=6:
            self.compound_kline_type(kline,ShapeType["中"].value*10000) #中阴、中阳
        else:
            self.compound_kline_type(kline,ShapeType["大"].value*10000) #大阴、大阳
        return {**kline}